//
//  AddGymAdminController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/12.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//


typedef NS_ENUM(NSInteger, AddGymAdminVCCell) {
    AddGymAdminVCCell_Account,//账号
    AddGymAdminVCCell_PWD,//密码
    AddGymAdminVCCell_Name,//姓名
    AddGymAdminVCCell_Sex,//性别
    AddGymAdminVCCell_Title,//职位
    AddGymAdminVCCell_Mobile,//联系电话
    AddGymAdminVCCell_Gym,//所在门店
};

#import "AddGymAdminController.h"
#import "CommonTableViewCell.h"
#import "I_Admin.h"

@interface AddGymAdminController ()<UITableViewDelegate,UITableViewDataSource,YDSwitchDelegate>
@property (nonatomic, strong) AddGymAdminSexCell *sexCell;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) M_Admin *admin;

@end

@implementation AddGymAdminController
+(instancetype)controller
{
    AddGymAdminController *vc = [UIStoryboard controllerWithStoryBoardIdentify:NSStringFromClass([AddGymAdminController class]) inStoryBoard:@"Group"];
    return vc;
}

- (AddGymAdminSexCell *)sexCell
{
    if (_sexCell == nil) {
        _sexCell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AddGymAdminSexCell class])];
        _sexCell.sexSwitchView.titles = @[@"男",@"女"];
        _sexCell.sexSwitchView.delegate = self;
    }
    return _sexCell;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加店长";
    [self setRightBarButtonWithTitle:@"保存" target:self action:@selector(creatGymAdmin)];
    _dataSource = @[@[@(AddGymAdminVCCell_Account),@(AddGymAdminVCCell_PWD)],@[@(AddGymAdminVCCell_Name),@(AddGymAdminVCCell_Sex),@(AddGymAdminVCCell_Title),@(AddGymAdminVCCell_Mobile),@(AddGymAdminVCCell_Gym)]];
    _admin = [[M_Admin alloc] init];
    _admin.userTitle = @"店长";
    _admin.roleId = AdminType_ShopManager;
    _admin.userSex = AdminSexType_Male;
    [self configUI];
}

#pragma mark UI
- (void)configUI
{
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = TP.controllerBackColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CommonTableViewCell class])];
    
}


- (void)refreshUI
{
    [self.tableView reloadData];
}

#pragma mark NET
- (void)creatGymAdmin
{
    WeakSelf(self);
    [AV showLoading:@"请稍后"];
    [I_Admin creatAdminWithAdmin:_admin success:^{
        [AV showSuccess:@"创建教练成功"];
        [weakself.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [AV showError:error];
    }];
}

#pragma mark Action




#pragma mark YDSwitchDelegate
- (void)choiceIndex:(NSInteger)index
{
    if (index == 0) {
        _admin.userSex = AdminSexType_Male;
    } else {
        _admin.userSex = AdminSexType_Female;
    }
    [self refreshUI];
}



#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = _dataSource[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CommonTableViewCell class])];
    AddGymAdminVCCell cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    switch (cellType) {
        case AddGymAdminVCCell_Account://账号
        {
            cell.showStyle = CommonTableViewShowStyleArrow_RightText;
            cell.leftLabel.text = @"账号";
            cell.rightLabel.textColor = HEXColor(@"4A4A4A");
            cell.rightLabel.text = _admin.account;
        }
            break;
        case AddGymAdminVCCell_PWD://密码
        {
            cell.showStyle = CommonTableViewShowStyleArrow_RightText;
            cell.leftLabel.text = @"密码";
            cell.rightLabel.textColor = HEXColor(@"4A4A4A");
            cell.rightLabel.text = _admin.accountPwd;
        }
            break;
        case AddGymAdminVCCell_Name://姓名
        {
            cell.showStyle = CommonTableViewShowStyleArrow_RightText;
            cell.leftLabel.text = @"姓名";
            cell.rightLabel.textColor = HEXColor(@"4A4A4A");
            cell.rightLabel.text = _admin.userName;
        }
            break;
        case AddGymAdminVCCell_Sex://性别
        {
            self.sexCell.sexSwitchView.index = (_admin.userSex == AdminSexType_Female) ? 1 : 0;
            return self.sexCell;
        }
            break;
        case AddGymAdminVCCell_Title://职位
        {
            cell.showStyle = CommonTableViewShowStyleRightText;
            cell.leftLabel.text = @"职位";
            cell.rightLabel.textColor = HEXColor(@"9B9B9B");
            cell.rightLabel.text = _admin.userTitle;
        }
            break;
        case AddGymAdminVCCell_Mobile://联系电话
        {
            cell.showStyle = CommonTableViewShowStyleArrow_RightText;
            cell.leftLabel.text = @"联系电话";
            cell.rightLabel.textColor = HEXColor(@"4A4A4A");
            cell.rightLabel.text = _admin.userMobile;
        }
            break;
        case AddGymAdminVCCell_Gym://所在门店
        {
            cell.showStyle = CommonTableViewShowStyleRightText;
            cell.leftLabel.text = @"所在门店";
            cell.rightLabel.textColor = HEXColor(@"9B9B9B");
            cell.rightLabel.text = RI.currentGym.gymName;
        }
            break;
            
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AddGymAdminVCCell cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    switch (cellType) {
        case AddGymAdminVCCell_Account://账号
        {
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_AdminAccount originStr:_admin.account];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                weakself.admin.account = editStr;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
            break;
        case AddGymAdminVCCell_PWD://密码
        {
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_AdminAccountPWD originStr:_admin.accountPwd];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                weakself.admin.accountPwd = editStr;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
            break;
        case AddGymAdminVCCell_Name://姓名
        {
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_AdminName originStr:_admin.userName];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                weakself.admin.userName = editStr;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
            break;
        case AddGymAdminVCCell_Sex://性别
        {
            
        }
            break;
        case AddGymAdminVCCell_Title://职位
        {
            
        }
            break;
        case AddGymAdminVCCell_Mobile://联系电话
        {
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_AdminMobile originStr:_admin.userMobile];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                weakself.admin.userMobile = editStr;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
            break;
        case AddGymAdminVCCell_Gym://所在门店
        {
            
        }
            break;
            
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 48;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc] init];
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"账号信息";
        titleLabel.textColor = HEXColor(@"4A4A4A");
        titleLabel.font = RegularFont(15);
        [view addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).offset(20);
            make.bottom.equalTo(view).offset(-5);
            make.height.equalTo(@(21));
        }];
        return view;
    } else {
        UIView *view = [[UIView alloc] init];
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"成员信息";
        titleLabel.textColor = HEXColor(@"4A4A4A");
        titleLabel.font = RegularFont(15);
        [view addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).offset(20);
            make.bottom.equalTo(view).offset(-5);
            make.height.equalTo(@(21));
        }];
        return view;
    }
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
@end


@implementation AddGymAdminSexCell

@end
